//find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。
const array1 = [5, 12, 8, 130, 44]

const found = array1.find((element) => element > 10)

console.log(found)
//用对象的属性查找数组里的对象
var inventory = [
  { name: 'apples', quantity: 2 },
  { name: 'bananas', quantity: 0 },
  { name: 'cherries', quantity: 5 },
]

function findCherries(fruit) {
  return fruit.name === 'cherries'
}

console.log(inventory.find(findCherries))

//寻找数组中的质数
function isPrime(element, index, array) {
  var start = 2
  while (start <= Math.sqrt(element)) {
    if (element % start++ < 1) {
      return false
    }
  }
  return element > 1
}

console.log([4, 6, 8, 12].find(isPrime)) // undefined, not found
console.log([4, 5, 8, 12].find(isPrime)) // 5
